package com.alumni_circle.core.web.access;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.access.AccessLog;
import com.alumni_circle.core.entity.access.VO.AccessLogSearchVO;
import com.alumni_circle.core.entity.rbac.Role;
import com.alumni_circle.core.service.access.IAccessLogService;
import com.alumni_circle.core.web.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙小虬
 * @since 2020-08-02 20:43
 */
@Api(tags = "访问日志")
@Controller
@RequestMapping("/accessLog")
public class AccessLogController extends BaseController<Role> {

    @Resource
    IAccessLogService accessLogService;


    /**
     * 查询所有角色信息 ，可分页查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllAccessLog", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有访问信息", notes = "查询所有访问信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有访问信息",url="/accessLog/getAllAccessLog")
    public ResponseListModel getAllAccessLog(@ApiParam(required = true,name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<AccessLog> list = accessLogService.list();
        System.out.println(list.size());
        PageInfo<AccessLog> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo.getTotal());
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/searchAccessLog", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索所有访问信息", notes = "搜索所有访问信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索所有访问信息",url="/accessLog/searchAccessLog")
    public ResponseListModel searchAccessLog(@ApiParam(required = true,name = "pageBase")
                                             @RequestBody AccessLogSearchVO accessLogSearchVO) {
        PageHelper.startPage(accessLogSearchVO.getPageNum(),accessLogSearchVO.getPageSize());
        List<AccessLog> list = accessLogService.list(new QueryWrapper<AccessLog>()
                .like("create_time",accessLogSearchVO.getCreateTime())
                .like("user_name",accessLogSearchVO.getUserName())
                .like("type",accessLogSearchVO.getType())
                .like("url",accessLogSearchVO.getUrl()));
        System.out.println(list.size());
        PageInfo<AccessLog> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo.getTotal());
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }
}
